
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>fuint会员营销管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <link rel="stylesheet" href="css/layui.css" >
<!--    <link rel="stylesheet" href="css/article.css">-->
    <style>
        /* 移动端 */
        @media screen and (max-width: 768px) {
            .layui-layout-admin .layui-layout-left,
            .layui-layout-admin .layui-body,
            .layui-layout-admin .layui-footer{left: 0;}
            .layui-layout-admin .layui-side{left: -300px;}
        }
    </style>
</head>
<body>

        <!-- 内容主体区域 -->
        <div>
            <!-- 内容主体区域  面包屑-->
            <span class="layui-breadcrumb">
              <a href="index.html">首页</a>
              <a><cite>店铺管理</cite></a>
              <a href="staffManage.html">员工管理</a>
            </span>

            <!-- 水平线 -->
            <hr class="layui-bg-blue">

            <!-- 搜索条件 -->
            <fieldset class="layui-elem-field">
                <legend>筛选条件</legend>
                <div class="layui-field-box">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">员工类别</label>
                        <div class="layui-input-inline">
                            <select name="aihao" lay-filter="aihao" id="search_type">
                                <option value="" selected=""></option>
                                <option value="0">店长</option>
                                <option value="1">收银员</option>
                                <option value="2">销售</option>
                                <option value="3">服务员</option>

                            </select>
                        </div>

                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_username" placeholder="请输入姓名" class="layui-input">
                        </div>

                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_phone" placeholder="请输入手机号" class="layui-input">
                        </div>

                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="search_status" lay-filter="search_status" id="search_status">
                                <option name="search_status" value="" selected=""></option>
                                <option name="search_status" value="1">启用</option>
                                <option name="search_status" value="0">禁用</option>
                            </select>
                        </div>

                    <div class="layui-form-item layui-form-pane">
                        <div class="layui-inline">

                            <button class="layui-btn layui-btn-radius layui-btn-normal" id="searchVal">搜索</button>
                            <button class="layui-btn layui-btn-radius layui-btn-warm" id="iReset">重置</button>
                            <button class="layui-btn layui-btn-radius layui-btn-normal" id="insertUser">新增用户</button>
                        </div>
                    </div>
                </div>
                </div>
            </fieldset>

            <table class="layui-hide" id="test" lay-filter="test"></table>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                </div>
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>

            <script type="text/html" id="insert_form">
                <div class="layui-field-box">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">员工类别</label>
                        <div class="layui-input-inline">
                            <select name="staff" lay-filter="staff" id="staff">
                                <option value="" selected=""></option>
                                <option value="0">店长</option>
                                <option value="1">收银员</option>
                                <option value="2">销售</option>
                                <option value="3">服务员</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_username" class="layui-input" placeholder="请输入用户名">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_password" class="layui-input" placeholder="请输入密码">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_phone" class="layui-input" placeholder="请输入手机号">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">所属店铺</label>
                        <div class="layui-input-inline">
                            <select name="shop" lay-filter="shop" id="shop">
                                <option value="0" selected=""></option>
                                <option value="1">人呀口腔</option>
                                <option value="2">洁美口腔</option>
                                <option value="3">百货商城一家</option>
                                <option value="4">胡O</option>
                                <option value="5">一家百货</option>
                                <option value="6">”IA和JNM</option>
                                <option value="7">tom</option>
                                <option value="8">jerry</option>
                                <option value="9">dandandan</option>
                                <option value="10">aCsvre</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="status" lay-filter="status" id="status">
                                <option value="0">禁用</option>
                                <option value="1" selected="">启用</option>
                            </select>
                        </div>
                    </div>

                </div>
            </script>

            <script type="text/html" id="update_form">
                <div class="layui-field-box">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">员工类别</label>
                        <div class="layui-input-inline">
                            <select name="staff" lay-filter="staff" id="ustaff">
                                <option value="" selected=""></option>
                                <option value="0">店长</option>
                                <option value="1">收银员</option>
                                <option value="2">销售</option>
                                <option value="3">服务员</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="update_username" class="layui-input" placeholder="请输入用户名">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-inline">
                            <input type="text" id="update_password" class="layui-input" placeholder="请输入密码">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">手机号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="update_phone" class="layui-input" placeholder="请输入手机号">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">所属店铺</label>
                        <div class="layui-input-inline">
                            <select name="shop" lay-filter="shop" id="ushop">
                                <option value="" selected=""></option>
                                <option value="1">人呀口腔</option>
                                <option value="2">洁美口腔</option>
                                <option value="3">百货商城一家</option>
                                <option value="4">胡O</option>
                                <option value="5">一家百货</option>
                                <option value="6">”IA和JNM</option>
                                <option value="7">tom</option>
                                <option value="8">jerry</option>
                                <option value="9">dandandan</option>
                                <option value="10">aCsvre</option>
                            </select>
                        </div>
                    </div>

                </div>
            </script>
        </div>

<script src="layui/layui.js" ></script>
<script src="jquery/jquery-3.6.0.js" ></script>
<script>
    var staffType=[
        '店长',
        '收银员',
        '销售',
        '服务员'
    ]
    var typeSeleced='';
    // console.log(staffType)
    // var istatus = $('input[name="status"]:checked').val();
    // console.log(istatus)


    $("#search_type").click(function () {
        var flag = $("#search_type").val()
        if (flag!=null)
            typeSeleced=staffType[flag]

    });

    // 弹出框员工类别点击事件
    $(document).on('click', '#staff', function() {
            var flag = $("#staff").val()
            if (flag!=null)
                typeSeleced=staffType[flag]

    });
    $(document).on('click', '#ustaff', function() {
            var flag = $("#ustaff").val()
            if (flag!=null)
                typeSeleced=staffType[flag]

    });
    //弹出框状态事件
    // $(document).on('click','#ustatus',function () {
    //     var ustatus = $('#ustatus input[name="ustatus"]:checked').val();
    //     console.log(ustatus)
    // })

    layui.use(['element', 'layer', 'util','form'], function(){
        var element = layui.element
            ,layer = layui.layer
            ,util = layui.util
            ,form = layui.form
            ,$ = layui.$;


        //头部事件
        util.event('lay-header-event', {
            //左侧菜单事件
            menuLeft: function(othis){
                layer.msg('展开左侧菜单的操作', {icon: 0});
            }
            ,menuRight: function(){
                layer.open({
                    type: 1
                    ,title: '更多'
                    ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
                    ,area: ['260px', '100%']
                    ,offset: 'rt' //右上角
                    ,anim: 5
                    ,shadeClose: true
                    ,scrollbar: false
                });
            }
        });

    });
    $("#insertUser").click(function () {
        layer.open({     // 弹出框处理  模态框
            type: 1               // 模态框
            ,title: '新增员工' //不显示标题栏
            ,closeBtn: false
            ,area: ['500px','500px']  // 宽和高
            ,shade: 0.5
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btn: ['新增', '取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: $('#insert_form').html()         // 引入自定义表单
            ,btn1: function(index, layero){            // 这个是新增按钮点击事件
                // layer.msg("新增按钮执行");
                // let type=$("#staff").val();
                let username = $("#insert_username").val();
                let password = $("#insert_password").val();
                let phone=$("#insert_phone").val();
                let status=$("#status").val();
                let shopId = $("#shop").val();
                $.ajax({
                    url:"/user/insertStaff",           // 请求路径
                    type:"post",                  // 请求的方式，不区分大小写
                    cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                    contentType:"application/json;charset=utf-8",
                    data: JSON.stringify({
                        type:typeSeleced,
                        username:username,
                        password:password,
                        phone:phone,
                        shopId:shopId,
                        status:status
                    }),
                    datatype:"json",        // 返回类型，text文本、html页面、json数据
                    success:function(response){
                        console.log("返回: " + response);
                        if(response.code === 200) {
                            layer.msg("新增用户成功!");
                            layer.close(index);                         // 关闭当前窗口
                            tableFlush();
                        }
                    },
                    error:function(response){
                        console.log("出错返回: " + response);
                    }
                });
            }
            ,success: function(layero){

            }
        });

    })


    let username = sessionStorage.getItem("username");

    $("#header_username").text(username);

    $("#iReset").click(function () {
        $("#search_username").val("");
        $("#search_type").val("");
        $("#search_phone").val("");
        $("#search_status").val("");
        tableFlush();
    });
// 0启用1禁用
    $("#searchVal").click(function () {
        let search_username = $("#search_username").val();
        let search_type = typeSeleced;
        let search_phone = $("#search_phone").val();
        let search_status = $("#search_status").val();

        console.log(search_status);
        let whereJSON = {};
        if(search_username != null && search_username != '') {
            whereJSON.username = search_username;
        }
        if(search_type != null && search_type != '') {
            whereJSON.type = search_type;
        }
        if(search_phone != null && search_phone != '') {
            whereJSON.phone = search_phone;
        }
        if(search_status != null && search_status != '') {
            whereJSON.status = search_status;
        }
        console.log(whereJSON)
        tableFlush(whereJSON);

    });


    function tableFlush(whereJSON) {
        layui.use('table', function(){
            var table = layui.table;
            //  render  渲染
            table.render({
                elem: '#test'
                ,url:'/user/selectByPage'
                ,where:whereJSON
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '用户数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{title:'ID', width:90, unresize: true, sort: true, templet: function(res){
                            return '<em>'+ res.id +'</em>'
                        }}
                    ,{title:'类别', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.type +'</em>'
                        }}
                    ,{title:'姓名', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.username +'</em>'
                        }}
                    ,{title:'密码', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.password +'</em>'
                        }}
                    ,{title:'手机号', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.phone +'</em>'
                        }}
                    ,{title:'关联会员ID', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.memberId +'</em>'
                        }}
                    ,{title:'所属店铺', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.shopname +'</em>'
                        }}
                    , {
                        title: '状态', width: 120, unresize: true, templet: function (res) {
                            let staffId = res.id;
                            if (res.status == 1) {
                                return "   <input id='kg' type='checkbox'  staffId = '" + staffId + "' lay-filter='status' lay-skin='switch' lay-text='ON|OFF' checked >"
                            } else if (res.status == 0) {
                                return "   <input id='kg' type='checkbox'  staffId = '" + staffId + "'  lay-filter='status' lay-skin='switch' lay-text='ON|OFF'>"
                            }
                        }}

                    ,{title:'创建时间', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.registerTime +'</em>'
                        }}
                    ,{title:'更新时间', width:90, unresize: true, templet: function(res){
                            return '<em>'+ res.updataTime +'</em>'
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true
            });

            // 状态按钮事件updateStaff
            layui.form.on('switch(status)', function (data) {
                // var status = this.status;
                // var name = this.name;
                // layer.tips(status + ' ' + name + ': '+ obj.elem.checked, obj.othis);
                this.checked?data.value=1:data.value=0;  //更改选中和未选中时，传动后台的值，此时选中传1，未选中传0。
                layer.msg('状态：' + (this.checked ? 'on' : 'off'), {
                    offset: '6px',
                    time: 500,
                });
                let staffId = data.elem.attributes['staffId'].nodeValue;
                console.log(staffId);

                // if(data.elem.checked){
                $.ajax({
                    url: "/user/updateStaff",           // 请求路径
                    type: "post",                  // 请求的方式，不区分大小写
                    cache: false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({
                        id:staffId,
                        status: data.value

                    }),
                    datatype: "json",        // 返回类型，text文本、html页面、json数据
                    success: function (response) {
                        console.log("返回: " + response);
                        if (response.code === 200) {
                            layer.msg("修改成功!");
                            //  layer.close(index);                         // 关闭当前窗口
                            // tableFlush();
                            table.reload('test', {});
                        }
                    },
                    error:function(response){
                        console.log("出错返回: " + response);
                    }
                });

                table.reload('test', {});
                var kg = data.value; //将该值传到后端即可使用。（如，直接在ajax中传值）
                console.log(kg);
                console.log(data);
                console.log(data.id);
                console.log(data.status);
                console.log(data.elem); //得到checkbox原始DOM对象
                console.log(data.elem.checked); //开关是否开启，true或者false
                console.log(data.value); //开关value值，也可以通过data.elem.value得到
                console.log(data.othis); //得到美化后的DOM对象
            });


            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                console.log(data)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.ajax({
                            url:"/user/deleteStaff",           // 请求路径
                            type:"post",                  // 请求的方式，不区分大小写
                            cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                            data: {
                                id:data.id
                            },
                            datatype:"json",        // 返回类型，text文本、html页面、json数据
                            success:function(response){
                                console.log("返回: " + response);
                                if(response.code === 200) {
                                    layer.msg("删除用户成功!");
                                    layer.close(index);                         // 关闭当前窗口
                                    tableFlush();
                                }
                            },
                            error:function(response){
                                console.log("出错返回: " + response);
                            }
                        });
                    });
                } else if(obj.event === 'edit'){
                    layer.open({     // 弹出框处理  模态框
                        type: 1               // 模态框
                        , title: '更新会员信息' //不显示标题栏
                        , closeBtn: false
                        , area: ['500px', '500px']  // 宽和高
                        , shade: 0.5
                        , id: 'LAY_layuipro' //设定一个id，防止重复弹出
                        , btn: ['更新', '取消']
                        , btnAlign: 'c'
                        , moveType: 1 //拖拽模式，0或者1
                        , content: $('#update_form').html()         // 引入自定义表单
                        , btn1: function (index, layero) {            // 这个是新增按钮点击事件
                            // console.log("111");
                            let id = data.id;
                            let username = $("#update_username").val();
                            let password = $("#update_password").val();
                            let phone = $("#update_phone").val();
                            let shopId = $("#ushop").val();
                            let status = $('#ustatus input[name="ustatus"]:checked').val();
                            // let status = $('#ustatus input[name="ustatus"]:checked').val();
                            $.ajax({
                                url: "/user/updateStaff",           // 请求路径
                                type: "post",                  // 请求的方式，不区分大小写
                                cache: false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                                contentType: "application/json;charset=utf-8",
                                data: JSON.stringify({
                                    id:id,
                                    type:typeSeleced,
                                    username:username,
                                    password:password,
                                    phone:phone,
                                    shopId:shopId,
                                    status:status
                                }),
                                datatype: "json",        // 返回类型，text文本、html页面、json数据
                                success: function (response) {
                                    console.log("返回: " + response);
                                    if (response.code === 200) {
                                        layer.msg("更新用户成功!");
                                        layer.close(index);                         // 关闭当前窗口
                                        tableFlush();
                                    }
                                },
                                error: function (response) {
                                    console.log("出错返回: " + response);
                                }
                            });
                        }
                        , success: function (layero) {

                        }
                    });
                }

            });
        });
    }

    tableFlush();
</script>

</body>
</html>
